import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';
import '../widget/main_item_widget.dart';
import '../model/main_view_model.dart';

Widget buildView(
    CommunityListViewState state, Dispatch dispatch, ViewService viewService) {
  List<String> sortTitles = ['全部', '待安检', '安检中', '已完成'];
  Widget _sortWidget() {
    List<Widget> items = sortTitles.map<Widget>((e) {
      int index = sortTitles.indexOf(e);
      return Expanded(
        child: Container(
          color:
              state.sortIndex == index ? ThemeConfig.mainColor : Colors.white,
          child: InkWell(
            child: Align(
              child: Text(
                e,
                style: TextStyle(
                  color: state.sortIndex == index
                      ? Colors.white
                      : ThemeConfig.blackColor,
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            onTap: () =>
                dispatch(CommunityListViewActionCreator.onTapSort(index)),
          ),
        ),
      );
    }).toList();

    return Container(
      margin: EdgeInsets.only(top: 0),
      height: 50,
      width: double.infinity,
      color: Colors.white,
      child: Row(
        children: items,
      ),
    );
  }

  Widget safeBuilder(BuildContext context, int index) {
    if (state.items.length == 0)
      return Container(
        child: Center(
          child: Stack(
            children: <Widget>[
              MyEmptyItemWidget(),
            ],
          ),
        ),
      );
    if (state.type == 1) {
      PatrolAreaModel item = state.items[index];
      return MainItemWidget.patrolBuilder(context, item, showTask: true);
    }
    CommunityItemModel item = state.items[index];
    return MainItemWidget.safeBuilder(context, item, showTask: true);
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: state.type == 1 ? '巡检任务' :'安检任务'),
    body: Column(
      children: [
        Container(
          color: ThemeConfig.mainColor,
          child: MySearchWidget(
            searchText: '搜索小区名',
            heroTag: 'listSearch',
            type: 3,
            workID: state.userID,
          ),
        ),
        _sortWidget(),
        Expanded(
          child: Container(
            child: state.items == null
                ? Center(
                    child: MyLoadingWidget(),
                  )
                : CustomRefreshList(
                    onRefresh: (_) =>
                        dispatch(CommunityListViewActionCreator.onLoad(1)),
                    currentPage: state.currentPage,
                    refreshController: state.refreshController,
                    child: ListView.builder(
                      itemBuilder: safeBuilder,
                      itemCount:
                          state.items.length == 0 ? 1 : state.items.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                    ),
                  ),
          ),
        ),
      ],
    ),
  );
}
